Gesture Detector

  • Code

    Gesture Detector in Flutter is used to detect the user’s gestures on the application. It is a non-visual widget.

    Inside the gesture detector, another widget is placed and the gesture detector will capture all these events (gestures) and execute the tasks accordingly. It notifies whenever the particular motion event has occurred.

    1- Tap:

    When the widget inside the gesture detector is tapped once, the function or the code inside the onTap() function will run.

    2- Double Tap:

    When the widget inside the gesture detector is tapped twice, the function or the code inside the onDoubleTap() function will run.

    3- Long Press:

    When the widget inside the gesture detector is pressed for a long time, the function or the code inside the onLongPress() function will run.

    
                      class _MyHomePageState extends State<MyHomePage> {
    
    
    bool _lightIsOn = false;
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Container(
          child: Column(
            children: <Widget>[
              Padding(
                child: Icon(
                  Icons.lightbulb_outline,
                  color: _lightIsOn ? Colors.yellow.shade600 : Colors.black,
                  size: 60,
                ),
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _lightIsOn = !_lightIsOn;
                    final snackBar = SnackBar(content: Text('Tap Function'));
                    ScaffoldMessenger.of(context).showSnackBar(snackBar);
                  });
                },
                onDoubleTap: (){
                  setState(() {
                    _lightIsOn = !_lightIsOn;
                    final snackBar = SnackBar(content: Text('Double Tap Function'));
                    ScaffoldMessenger.of(context).showSnackBar(snackBar);
                  });
                },
                onLongPress: (){
                  setState(() {
                    _lightIsOn = !_lightIsOn;
                    final snackBar = SnackBar(content: Text('Long Press Function'));
                    ScaffoldMessenger.of(context).showSnackBar(snackBar);
                  });
                },
                  child: Text(_lightIsOn ? 'TURN LIGHT OFF' : 'TURN LIGHT ON'),
                ),
              ),
            ],
          ),
        ),
      );
    }
    }